<template>
	<el-main>
		<el-row :gutter="15">
			<el-col :xl="8" :lg="8" :md="12" :sm="12" :xs="24" v-for="item in list" :key="item.uuid">
				<el-card class="task task-item" shadow="hover">
					<h2>{{ item.taskName }}</h2>
					<ul>
						<li>
							<h4>执行类</h4>
							<p>{{ item.invokeTarget }}</p>
						</li>
						<li>
							<h4>定时规则</h4>
							<p>{{ item.cronExpression }}</p>
						</li>
					</ul>
					<div class="bottom">
						<div class="state">
							<el-tag v-if="item.status===99" size="small">任务已禁用</el-tag>
							<el-tag v-if="item.status===0" size="small">执行失败</el-tag>
							<el-tag v-if="item.status===1" size="small">任务已启用</el-tag>
							<el-tag v-if="item.status===10" size="small">任务运行中</el-tag>
							<el-tag v-if="item.status===2" size="small">运行完成</el-tag>
							<el-tag v-if="item.status===3" size="small">取消运行</el-tag>
						</div>
						<div class="state">
							<el-tag v-if="item.tag==='internalFuncTask'" size="small">内置函数任务</el-tag>
							<el-tag v-if="item.tag==='httpTask'" size="small">HTTP任务</el-tag>
							<el-tag v-if="item.tag==='shellTask'" size="small">shell任务</el-tag>
						</div>
						<div class="handler">
							<el-popconfirm title="确定立即执行一次吗？" @confirm="run(item)">
								<template #reference>
									<el-button type="primary" icon="el-icon-caret-right" circle></el-button>
								</template>
							</el-popconfirm>
							<el-dropdown trigger="click">
								<el-button type="primary" icon="el-icon-more" circle plain></el-button>
								<template #dropdown>
									<el-dropdown-menu>
										<el-dropdown-item @click="edit(item)">编辑</el-dropdown-item>
										<el-dropdown-item @click="logs(item)">日志</el-dropdown-item>
										<el-dropdown-item @click="del(item)" divided>删除</el-dropdown-item>
									</el-dropdown-menu>
								</template>
							</el-dropdown>
						</div>
					</div>
				</el-card>
			</el-col>
			<el-col :xl="6" :lg="6" :md="8" :sm="12" :xs="24">
				<el-card class="task task-add" shadow="never" @click="add">
					<el-icon>
						<el-icon-plus/>
					</el-icon>
					<p>添加计划任务</p>
				</el-card>
			</el-col>
		</el-row>
	</el-main>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>

	<el-drawer :title="taskLogItem.taskName" v-model="dialog.logsVisible" :size="600" direction="rtl" destroy-on-close>
		<el-container>
			<el-main style="padding:0 20px;">
				<scTable ref="table" :api-obj="apiObj" :params="params" stripe>
					<el-table-column label="执行时间" prop="startTime" width="200"></el-table-column>
					<el-table-column label="结束时间" prop="endTime" width="200"></el-table-column>
					<el-table-column label="执行结果" prop="status" width="100">
						<template #default="scope">
							<!--							Disabled SysTaskStatus = 99 // 禁用-->
							<!--							Failure  SysTaskStatus = 0  // 失败-->
							<!--							Enabled  SysTaskStatus = 1  // 启用-->
							<!--							Running  SysTaskStatus = 10 // 运行中-->
							<!--							Finish   SysTaskStatus = 2  // 完成-->
							<!--							Cancel   SysTaskStatus = 3  // 取消-->
							<span v-if="scope.row.status===2" style="color: #67C23A;"><el-icon><el-icon-success-filled/></el-icon></span>
							<span v-else style="color: #F56C6C;"><el-icon><el-icon-circle-close-filled/></el-icon></span>
						</template>
					</el-table-column>
					<el-table-column label="执行日志" prop="logs" width="100" fixed="right">
						<template #default="scope">
							<el-button size="small" @click="showLogDetail(scope.row)" type="text">日志</el-button>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-drawer>

	<el-drawer title="日志" v-model="dialog.logDetail" :size="500" direction="rtl" destroy-on-close>
		<el-main style="padding:0 20px 20px 20px;">
			<pre
				style="font-size: 12px;color: #999;padding:20px;background: #333;font-family: consolas;line-height: 1.5;overflow: auto;">{{
					taskLogResult
				}}</pre>
		</el-main>
	</el-drawer>
</template>

<script>
import saveDialog from './save'

export default {
	name: 'task',
	components: {
		saveDialog,
	},
	provide() {
		return {
			list: this.list
		}
	},
	data() {
		return {
			params: {
				uuid: ''
			},
			taskLogItem: {},
			taskLogResult: '',
			apiObj: this.$API.system.TaskLog.getTaskLogByID,
			dialog: {
				save: false,
				logsVisible: false,
				logDetail: false
			},
			list: []
		}
	},
	mounted() {
		this.getAllTask()
	},
	methods: {
		async getAllTask() {
			let res = await this.$API.system.Tasks.getSysTaskList.get()
			if (res.code === 0) {
				this.list = res.data.taskList
			} else {
				this.$message.error(res.message)
			}
		},
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		edit(task) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(task)
			})
		},
		del(task) {
			this.$confirm(`确认删除 ${task.title} 计划任务吗？`, '提示', {
				type: 'warning',
				confirmButtonText: '删除',
				confirmButtonClass: 'el-button--danger'
			}).then(() => {
				this.list.splice(this.list.findIndex(item => item.id === task.id), 1)
			}).catch(() => {
				//取消
			})
		},
		logs(item) {
			this.taskLogItem = item
			this.params.uuid = item.uuid
			this.dialog.logsVisible = true
		},
		showLogDetail(item) {
			this.taskLogResult = item.result
			this.dialog.logDetail = true
		},
		async run(task) {
			let res = await this.$API.system.Tasks.runOneSysTask.get({uuid: task.uuid})
			if (res.code === 0) {
				this.$message.success(`已成功执行计划任务：${task.title}`)
			} else {
				this.$message.error(res.message)
			}
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode === 'add') {
				data.id = new Date().getTime()
				this.list.push(data)
			} else if (mode === 'edit') {
				this.list.filter(item => item.id === data.id).forEach(item => {
					Object.assign(item, data)
				})
			}
		}
	}
}
</script>

<style scoped>
.task {
	height: 210px;
}

.task-item h2 {
	font-size: 15px;
	color: #3c4a54;
	padding-bottom: 15px;
}

.task-item li {
	list-style-type: none;
	margin-bottom: 10px;
}

.task-item li h4 {
	font-size: 12px;
	font-weight: normal;
	color: #999;
}

.task-item li p {
	margin-top: 5px;
}

.task-item .bottom {
	border-top: 1px solid #EBEEF5;
	text-align: right;
	padding-top: 10px;
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.task-add {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	text-align: center;
	cursor: pointer;
	color: #999;
}

.task-add:hover {
	color: #409EFF;
}

.task-add i {
	font-size: 30px;
}

.task-add p {
	font-size: 12px;
	margin-top: 20px;
}

.dark .task-item .bottom {
	border-color: var(--el-border-color-light);
}
</style>
